<template>
  <!-- 文件上传 -->
  <div>
    <el-upload
      action="http://learnspringcloud.oss-cn-beijing.aliyuncs.com"
      :data="dataObj"
      list-type="picture-card"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>


<script>


  import {mapGetters} from 'vuex'

  export default {
    name: 'UploadFile',
    props: {
      prop: null,
    },
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        fileList: [],
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: '',
          // callback:'',
        },
      }
    },
    methods: {
      beforeUpload(file) {
        return new Promise(((resolve, reject) => {
          this.$http({
            url: this.$http.getRequest('oss/getPolicy'),
            method: 'get',
            params: this.$http.adornParams()
          }).then((response) => {
            this.dataObj.policy = response.data.policy;
            this.dataObj.signature = response.data.signature;
            this.dataObj.ossaccessKeyId = response.data.accessid;
            this.dataObj.key = response.data.dir + '/' + this.getUUID() + file.name;
            this.dataObj.dir = response.data.dir;
            this.dataObj.host = response.data.host;
            resolve(true)
          }).catch(err => {
            reject(false)
          })
        }))
      },

      handlePreview(file) {
        this.dialogVisible = true;
        this.dialogImageUrl = file.url;
      },

      handleUploadSuccess(res, file,fileList) {
        console.log('文件上传成功！ 文件路径=',file,fileList)
        fileList.forEach(k =>{
          if (k.raw.uid === file.raw.uid) {
            k.url = window.SITE_CONFIG.imageUrl+this.dataObj.key
          }
        })
        this.$emit('beachUploadSuccess',this.prop,fileList)
      },

      getUUID() {
        let s = [];
        let hexDigits = "0123456789abcdef";
        for (let i = 0; i < 36; i++) {
          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        let uuid = s.join("");
        return uuid;
      },

      handleRemove(file, fileList) {
        this.$emit('beachRemove',this.prop,fileList)
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },

    }


  }
</script>

<style scoped>
  .disabled .el-upload--picture-card {
    display: none;
  }
</style>
